package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"prod-mgmt/cache"
	"prod-mgmt/models"
	"strconv"
	"time"
)

type ShopController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (s *ShopController) Add() {
	img := s.GetString("img")
	name := s.GetString("name")
	content := s.GetString("content")
	price, _ := s.GetFloat("price")
	shop := models.Shop{
		Img:     img,
		Name:    name,
		Content: content,
		Price:   price,
	}
	dataAdd := "data_add"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataAdd).Result()
	if cacheJson == "" {
		models.DB.Model(&models.Shop{}).Create(&shop)
		Json, _ := json.Marshal(&shop)
		cache.Rdb.Set(cache.Ctx, dataAdd, Json, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &shop)
		if err != nil {
			s.Data["json"] = Res{
				Code: 0,
				Msg:  "添加失败",
			}
			s.ServeJSON()
			return
		}
	}
	s.Data["json"] = Res{
		Code: 200,
		Msg:  "添加成功",
	}
	s.ServeJSON()
}

func (s *ShopController) Upload() {

	file, heard, err := s.GetFile("img")
	if err != nil {
		s.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
		}
		s.ServeJSON()
		return
	}
	if heard.Size > 2*1024*1024 {
		s.Data["json"] = Res{
			Code: 0,
			Msg:  "文件过大",
		}
		s.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + heard.Filename
	err = s.SaveToFile("img", path)
	if err != nil {
		s.Data["json"] = Res{
			Code: 0,
			Msg:  "文件上传失败",
		}
		s.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		s.Data["json"] = Res{
			Code: 200,
			Msg:  "文件上传成功",
			Data: Url,
		}
		s.ServeJSON()
	}
}
func (s *ShopController) List() {

	dataList := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataList).Result()
	var shop []models.Shop
	if cacheJson == "" {
		models.DB.Model(&models.Shop{}).Find(&shop)
		Json, _ := json.Marshal(&shop)
		cache.Rdb.Set(cache.Ctx, dataList, Json, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &shop)
		if err != nil {
			s.Data["json"] = Res{
				Code: 0,
				Msg:  "查询失败",
			}
			s.ServeJSON()
			return
		}
	}
	s.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: shop,
	}
	s.ServeJSON()
}
func (s *ShopController) Update() {
	id, _ := s.GetInt("id")
	img := s.GetString("img")
	name := s.GetString("name")
	content := s.GetString("content")
	price, _ := s.GetFloat("price")
	shop := models.Shop{
		Img:     img,
		Name:    name,
		Content: content,
		Price:   price,
	}
	dataUpdate := "data_update" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataUpdate).Result()
	if cacheJson == "" {
		models.DB.Model(&models.Shop{}).Where("id=?", id).Updates(&shop)
		Json, _ := json.Marshal(&shop)
		cache.Rdb.Set(cache.Ctx, dataUpdate, Json, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &shop)
		if err != nil {
			s.Data["json"] = Res{
				Code: 0,
				Msg:  "修改失败",
			}
			s.ServeJSON()
			return
		}
	}
	s.Data["json"] = Res{
		Code: 200,
		Msg:  "修改成功",
	}
	s.ServeJSON()
}
func (s *ShopController) Del() {

	id, _ := s.GetInt("id")
	dataDel := "data_del" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataDel).Result()
	if cacheJson == "" {
		models.DB.Model(&models.Shop{}).Where("id=?", id).Delete(&models.Shop{})
		Json, _ := json.Marshal(&models.Shop{})
		cache.Rdb.Set(cache.Ctx, dataDel, Json, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &models.Shop{})
		if err != nil {
			s.Data["json"] = Res{
				Code: 0,
				Msg:  "删除失败",
				Data: nil,
			}
			s.ServeJSON()
			return
		}
	}
	s.Data["json"] = Res{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	s.ServeJSON()
}
